import toolMgr from '@/biz/base/tool.js'
let _this = null
import ruleOpt from '@/biz/option/rule.js'

function options1(vm) {
  _this = vm
  return [
    accessoryImg(_this.acBasicsReq),
    {
      title: '页面标题',
      key: 'titleTag',
      disabled: _this.statusLastFlag,
      placeholder: '参与活动赢好礼',
      input: {
        type: 'text',
        maxlength: 10
      },
      rules: [
        {
          required: true,
          message: '请输入页面标题',
          trigger: 'blur'
        }
      ]
    },
    accessoryImg2(_this.acBasicsReq),
    accessoryImg4(_this.acBasicsReq),
    {
      title: '活动时间',
      key: 'activityTime',
      disabled: _this.statusLastFlag,
      placeholder: '请选择活动时间',
      xl: 4,
      date: {
        type: 'datetimerange',
        format: 'yyyy-MM-dd HH:mm:ss',
        disabled: (date) => {
          return date && date.valueOf() < Date.now() - 86400000
        }
      },
      rules: [
        {
          type: 'array',
          required: true,
          message: '请选择活动时间',
          trigger: 'blur'
        }
      ]
    },
    {
      title: '活动说明',
      key: 'reason',
      disabled: _this.statusLastFlag,
      placeholder: '请输入活动说明',
      tips: '活动介绍、奖品介绍、参与限制、兑奖方式、活动解释等',
      editor: {
        type: 'toolbarList2'
      },
      rules: [
        {
          required: true,
          message: '请输入活动说明',
          trigger: 'blur'
        }
      ]
    }
  ]
}

function options2(vm, flag = false) {
  _this = vm
  let opt = {
    title: '奖品单价',
    key: 'unitPrice',
    disabled: _this.statusLastFlag,
    visible: flag,
    placeholder: '请输入奖品单价',
    input: {
      type: 'number'
    },
    rules: [
      {
        type: 'number',
        required: true,
        message: '请输入奖品单价',
        trigger: 'blur'
      },
      ruleOpt.minNum(0)
    ]
  }
  // let opt1 = {
  //   key: 'prizeApi',
  //   col: 20,
  //   tips: '请填写接口地址，系统会根据接口判断用户是否有该奖品的中奖资格',
  //   disabled: _this.statusLastFlag,
  //   visible: _this.acPrizeReqList[_this.tabsChoose].prizeAudit,
  //   input: {}
  // }
  let opt2 = {
    key: 2,
    value: '是否匿名展示',
    disabled: !_this.acPrizeReqList[_this.tabsChoose].showPrize.includes(1)
  }
  return [
    {
      title: '奖品类型',
      key: 'type',
      disabled: _this.statusLastFlag,
      radio: {
        options: [
          {
            key: 1,
            value: '实物'
          },
          {
            key: 2,
            value: '现金'
          }
        ]
      },
      change: (val) => {
        console.log('奖品类型', val)
        if (val.value == 2) {
          opt.visible = true
        } else {
          opt.visible = false
        }
      },
      rules: [ruleOpt.required('请选择奖品类型', 'number')]
    },
    {
      title: '奖品名称',
      key: 'name',
      disabled: _this.statusLastFlag,
      tips: _this.acPrizeReqList[_this.tabsChoose].name
        ? '若替换奖品，请勿直接修改名称，请删除后重新添加新奖品'
        : '',
      tipsColor: '#ed4014',
      placeholder: '请输入奖品名称(最大8个字符)',
      input: {
        type: 'text',
        maxlength: 8
      },
      rules: [
        {
          required: true,
          message: '请输入奖品名称',
          trigger: 'blur'
        }
      ]
    },
    accessoryImg5(),
    opt,
    {
      title: '奖品数量',
      key: 'count',
      disabled: _this.statusLastFlag,
      placeholder: '请输入奖品数量',
      input: {
        type: 'number'
      },
      change: (res) => {
        console.log(res, _this.acPrizeReqList, '111111111')
        let countFlag = _this.acPrizeReqList.some((item) => {
          return item.count > 0
        })

        if (!countFlag) {
          _this.$Message.warning('至少有一个奖品的数量不为0')
        }
      },
      rules: [
        {
          type: 'number',
          required: true,
          message: '请输入奖品数量',
          trigger: 'blur'
        },
        ruleOpt.minNumZero(0)
      ]
    },
    {
      title: '兑奖说明',
      key: 'exchangeReason',
      disabled: _this.statusLastFlag,
      placeholder: '请输入兑奖说明',
      tips: '请填写兑换方法、兑换条件等信息',
      input: {
        type: 'text',
        maxlength: 50
      },
      rules: [
        {
          required: true,
          message: '请输入兑奖说明',
          trigger: 'blur'
        }
      ]
    },
    {
      title: '中奖展示',
      key: 'showPrize',
      disabled: _this.statusLastFlag,
      checkbox: {
        options: [{ key: 1, value: '中奖后跑马灯展示中奖人信息' }, opt2]
      },
      change: (res) => {
        console.log(res, '111111111')
        if (res.value.includes(1)) {
          opt2.disabled = false
        } else {
          opt2.disabled = true
        }
      }
    },
    {
      title: '中奖审核接口',
      key: 'interfaceEnable',
      group: [
        {
          key: 'interfaceEnable',
          col: 4,
          disabled: _this.statusLastFlag,
          switch: {
            openText: '启用',
            closeText: '关闭',
            size: 'large'
          }
          // change: (val) => {
          //   if (val.value) {
          //     opt1.visible = true
          //   } else {
          //     opt1.visible = false
          //   }
          // }
        }
        // opt1
      ]
    }
  ]
}

function options3(vm) {
  _this = vm
  return [
    {
      title: '综合中奖率',
      key: 'synthesisWinningRate',
      group: [
        {
          key: 'synthesisWinningRate',
          col: 20,
          disabled: _this.statusLastFlag,
          placeholder: '请输入综合中奖率',
          tips: '请填写本次活动的预计中奖率，如人人都可获奖，综合中奖率为100%',
          input: {
            type: 'number'
          }
        },
        {
          placeholder: '%',
          col: 4,
          text: {}
        }
      ],
      rules: [
        {
          type: 'number',
          required: true,
          message: '请输入综合中奖率',
          trigger: 'blur'
        },
        ruleOpt.twoDecimal(),
        ruleOpt.percent(),
        ruleOpt.minNum(0)
      ]
    },
    {
      title: '初始抽奖次数',
      key: 'initialLotteryCount',
      disabled: _this.statusLastFlag,
      group: [
        {
          key: 'initialLotteryCount',
          disabled: _this.statusLastFlag,
          col: 20,
          placeholder: '请输入抽奖次数',
          input: {
            type: 'number'
          }
        },
        {
          placeholder: '次',
          col: 4,
          text: {}
        }
      ],
      rules: [
        {
          type: 'number',
          required: true,
          message: '请输入抽奖次数',
          trigger: 'blur'
        },
        ruleOpt.positiveNumber()
      ]
    },
    winningCountFlag(),
    winningNumFlag(),
    winningFlag()
  ]
}

function options4(vm) {
  _this = vm
  return assistAll(_this)
}
export default {
  options1,
  options2,
  options3,
  options4
}

function winningCountFlag() {
  let opt = {
    title: '每人最多可中奖',
    key: 'winningCount',
    disabled: _this.statusLastFlag,
    visible: _this.acLotteryReq.everyoneWinning == '1',
    group: [
      {
        key: 'winningCount',
        disabled: _this.statusLastFlag,
        col: 10,
        placeholder: '请输入中奖次数',
        input: {
          type: 'number'
        }
      },
      {
        placeholder: '次',
        col: 4,
        text: {}
      }
    ],
    rules: [ruleOpt.required('请输入中奖次数', 'number'), ruleOpt.positiveInteger()]
  }
  return [
    {
      title: '中奖次数',
      key: 'everyoneWinning',
      disabled: _this.statusLastFlag,
      col: 9,
      radio: {
        options: [
          { key: '0', value: '不限' },
          { key: '1', value: '限制' }
        ]
      },
      change: (val) => {
        if (val.value == 1) {
          opt.visible = true
        } else {
          opt.visible = false
        }
      },
      rules: [
        {
          required: true,
          message: '请选择中奖次数',
          trigger: 'blur'
        }
      ]
    },
    opt
  ]
}

function winningNumFlag() {
  let opt = {
    title: '整个活动(非单人)每日最多中奖',
    key: 'everydayWinningCount',
    disabled: _this.statusLastFlag,
    visible: _this.acLotteryReq.everydayWinning == '1',
    labelWidth: 240,
    group: [
      {
        key: 'everydayWinningCount',
        disabled: _this.statusLastFlag,
        col: 20,
        placeholder: '整个活动(非单人)每日最多中奖',
        input: {
          type: 'number'
        }
      },
      {
        placeholder: '次',
        col: 4,
        text: {}
      }
    ],
    rules: [
      {
        type: 'number',
        required: true,
        message: '请输入整个活动(非单人)每日最多中奖',
        trigger: 'blur'
      },
      ruleOpt.positiveInteger(),
      ruleOpt.maxNum(10000)
    ]
  }
  return [
    {
      title: '每日中奖次数',
      key: 'everydayWinning',
      disabled: _this.statusLastFlag,
      col: 9,
      radio: {
        options: [
          { key: '0', value: '不限' },
          { key: '1', value: '限制' }
        ]
      },
      change: (val) => {
        if (val.value == 1) {
          opt.visible = true
        } else {
          opt.visible = false
        }
      },
      rules: [
        {
          required: true,
          message: '请选择每日中奖次数',
          trigger: 'blur'
        }
      ]
    },
    opt
  ]
}

function winningFlag() {
  let opt = {
    title: '活动参加',
    key: 'winningLimitCount',
    disabled: _this.statusLastFlag,
    visible: _this.acLotteryReq.winningLimit == '1',
    group: [
      {
        key: 'winningLimitCount',
        disabled: _this.statusLastFlag,
        col: 4,
        placeholder: '请输入活动参加',
        input: {
          type: 'number'
        }
      },
      {
        placeholder: '次后才可中奖',
        col: 16,
        text: {}
      }
    ],
    rules: [
      {
        type: 'number',
        required: true,
        message: '请输入活动参加次数',
        trigger: 'blur'
      },
      ruleOpt.positiveInteger()
    ]
  }

  return [
    {
      title: '中奖限制',
      key: 'winningLimit',
      disabled: _this.statusLastFlag,
      col: 9,
      radio: {
        options: [
          { key: '0', value: '不限' },
          { key: '1', value: '限制' }
        ]
      },
      change: (val) => {
        if (val.value == '1') {
          opt.visible = true
        } else {
          opt.visible = false
        }
      },
      rules: [
        {
          required: true,
          message: '请选择中奖限制',
          trigger: 'blur'
        }
      ]
    },
    opt
  ]
}

function assistAll(_this) {
  let opt = {
    title: '最多获得抽奖次数',
    key: 'shareLimitCount',
    disabled: _this.statusLastFlag,
    visible: _this.taskOpt.isHelp && _this.taskOpt.limitCount == '1',
    col: 14,
    group: [
      {
        key: 'shareLimitCount',
        disabled: _this.statusLastFlag,
        col: 10,
        placeholder: '请输入中奖次数',
        input: {
          type: 'number'
        }
      },
      {
        placeholder: '次',
        col: 4,
        text: {}
      }
    ],
    rules: [ruleOpt.required('请输入抽奖次数', 'number'), ruleOpt.positiveInteger()]
  }
  let assistOpt = [
    {
      title: '任务动作',
      key: 'taskAction',
      disabled: _this.statusLastFlag,
      visible: _this.taskOpt.isHelp,
      select: {
        options: [{ key: 'CLUE', value: '线索录入' }]
      },
      rules: [
        {
          required: true,
          message: '请选择任务动作',
          trigger: 'blur'
        }
      ]
    },
    {
      title: '获取抽奖次数',
      key: 'shareCount',
      disabled: _this.statusLastFlag,
      visible: _this.taskOpt.isHelp,
      placeholder: '请输入抽奖次数',
      input: {
        type: 'number'
      },
      rules: [
        {
          type: 'number',
          required: true,
          message: '请输入抽奖次数',
          trigger: 'blur'
        },
        ruleOpt.positiveInteger()
      ]
    },
    [
      {
        title: '获得次数上限',
        key: 'limitCount',
        disabled: _this.statusLastFlag,
        visible: _this.taskOpt.isHelp,
        col: 10,
        radio: {
          options: [
            { key: '0', value: '不限' },
            { key: '1', value: '限制' }
          ]
        },
        change: (val) => {
          console.log(123123, val)
          if (val.value == '1') {
            opt.visible = true
          } else {
            opt.visible = false
          }
        },
        rules: [
          {
            required: true,
            message: '请选择次数上限',
            trigger: 'blur'
          }
        ]
      },
      opt
    ]
  ]
  let arr = [
    {
      title: '系统任务',
      key: 'isHelp',
      disabled: _this.statusLastFlag,
      tips: '启用后，完成系统任务自动获得额外抽奖次数',
      tipsIsBlock: 'Dinline',
      switch: {
        openText: '启用',
        closeText: '关闭',
        size: 'large'
      },
      change: (res) => {
        if (res.value) {
          assistOpt[0].visible = true
          assistOpt[1].visible = true
          assistOpt[2][0].visible = true
          if (_this.taskOpt.limitCount == '1') {
            opt.visible = true
          } else {
            opt.visible = false
          }
        } else {
          assistOpt[0].visible = false
          assistOpt[1].visible = false
          assistOpt[2][0].visible = false
          opt.visible = false
        }
      }
    },
    ...assistOpt
  ]

  return arr
}

function accessoryImg(info) {
  let api = toolMgr.unloadFile()
  let files = []
  if (info.accessoryBOList1 && info.accessoryBOList1.length > 0) {
    info.accessoryBOList1.forEach((item) => {
      if (item) {
        files.push({
          name: item.accessoryName,
          url: item.accessoryUrl
        })
      }
    })
  }
  let opt = {
    title: '上传',
    api,
    files,
    multiple: false,
    // disabled: false,
    show: false,
    // hideDel: false,
    hideDel: _this.statusLastFlag,
    maxlength: 1,
    hasCover: false,
    format: ['jpg', 'jpeg', 'png'],
    formatErr: () => {
      this.loading = false
      this.$Message.error('只支持上传jpg、jpeg、png图片！')
    }
    // before: (file) => {
    //   console.log(file, '上传前提前校验图片宽高');
    //   return _this.checkWH(file)
    // },
    // preview: (file) => {
    //   toolMgr.downloadFile(file.url, file.name)
    // }
  }
  return {
    title: '活动封面图',
    key: 'accessoryBOList1',
    disabled: _this.statusLastFlag,
    placeholder: '若封面图不上传，则直接进入活动页，建议上传1125*2172px图片',
    uploadImg: opt,
    data: {
      to: (v) => {
        let accessoryBOList1 = v.map((item) => {
          return {
            name: item.name,
            url: item.response ? item.response.data.imageURL : item.url,
            accessoryName: item.name,
            accessoryUrl: item.response ? item.response.data.imageURL : item.url
          }
        })
        return {
          accessoryBOList1
        }
      }
    }
    // rules: [{
    //   required: true,
    //   message: '请上传图片',
    //   type: 'array',
    //   trigger: 'blur'
    // }]
  }
}

function accessoryImg2(info) {
  let api = toolMgr.unloadFile()
  let files = []
  if (info.accessoryBOList2 && info.accessoryBOList2.length > 0) {
    info.accessoryBOList2.forEach((item) => {
      if (item) {
        files.push({
          name: item.accessoryName,
          url: item.accessoryUrl
        })
      }
    })
  }

  let opt = {
    title: '上传',
    api,
    files,
    multiple: true,
    // disabled: false,
    show: false,
    hideDel: _this.statusLastFlag,
    maxlength: 1,
    hasCover: false,
    format: ['jpg', 'jpeg', 'png'],
    formatErr: () => {
      this.loading = false
      this.$Message.error('只支持上传jpg、jpeg、png图片！')
    },
    preview: (file) => {
      toolMgr.downloadFile(file.url, file.name)
    }
  }
  return [
    {
      title: '顶部样式',
      key: 'accessoryBOList2',
      disabled: _this.statusLastFlag,
      col: 9,
      uploadImg: opt,
      data: {
        to: (v) => {
          let accessoryBOList2 = v.map((item) => {
            return {
              name: item.name,
              url: item.response ? item.response.data.imageURL : item.url,
              accessoryName: item.name,
              accessoryUrl: item.response ? item.response.data.imageURL : item.url
            }
          })
          return {
            accessoryBOList2
          }
        }
      }
    },
    {
      button: {
        type: 'text',
        title: '示意模板',
        tips: '请上传750*908px，且底部为纯色的图片',
        style: {
          color: '#4A6DDB'
        },
        click: () => {
          console.log(666111, _this.showModal)
          _this.showModal()
        }
      }
    }
  ]
}

function accessoryImg4(info) {
  let api = toolMgr.unloadFile()
  let files = []
  if (info.accessoryBOList4 && info.accessoryBOList4.length > 0) {
    info.accessoryBOList4.forEach((item) => {
      if (item) {
        files.push({
          name: item.accessoryName,
          url: item.accessoryUrl
        })
      }
    })
  }

  let opt = {
    title: '上传',
    api,
    files,
    multiple: true,
    show: false,
    hideDel: _this.statusLastFlag,
    drag: !_this.statusLastFlag,
    maxlength: 20,
    hasCover: false,
    format: ['jpg', 'jpeg', 'png'],
    formatErr: () => {
      this.loading = false
      this.$Message.error('只支持上传jpg、jpeg、png图片！')
    },
    preview: (file) => {
      toolMgr.downloadFile(file.url, file.name)
    }
  }
  return {
    title: '图片展示',
    key: 'accessoryBOList4',
    disabled: _this.statusLastFlag,
    placeholder: '可上传多张图片',
    uploadImg: opt,
    data: {
      to: (v) => {
        console.log(v, 'dddddddddd')
        let accessoryBOList4 = v.map((item) => {
          return {
            name: item.name,
            url: item.response ? item.response.data.imageURL : item.url,
            accessoryName: item.name,
            accessoryUrl: item.response ? item.response.data.imageURL : item.url
          }
        })
        return {
          accessoryBOList4
        }
      }
    }
  }
}

function accessoryImg5() {
  let api = toolMgr.unloadFile()
  let opt = {
    title: '上传',
    api,
    files: [],
    multiple: false,
    show: false,
    checkFilesLength: true,
    hideDel: _this.statusLastFlag,
    drag: true,
    maxlength: 1,
    hasCover: false,
    format: ['jpg', 'jpeg', 'png'],
    formatErr: () => {
      this.loading = false
      this.$Message.error('只支持上传jpg、jpeg、png图片！')
    },
    preview: (file) => {
      toolMgr.downloadFile(file.url, file.name)
    }
  }
  return {
    title: '奖品图案',
    key: 'accessoryBOList5',
    disabled: _this.statusLastFlag,
    placeholder: '建议上传200*200图片，若不上传则使用默认图',
    uploadImg: opt,
    data: {
      from: (info) => {
        let files = []
        if (info.accessoryBOList5 && info.accessoryBOList5.length > 0) {
          info.accessoryBOList5.forEach((item) => {
            if (item) {
              files.push({
                name: item.accessoryName,
                url: item.accessoryUrl
              })
            }
          })
        }
        opt.files = files
      },
      to: (v) => {
        let accessoryBOList5 = v.map((item) => {
          return {
            name: item.name,
            url: item.response ? item.response.data.imageURL : item.url,
            accessoryName: item.name,
            accessoryUrl: item.response ? item.response.data.imageURL : item.url
          }
        })
        return {
          accessoryBOList5
        }
      }
    }
  }
}
